package cn.sdhuijin.healthexam.activity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import cn.sdhuijin.healthexam.ExitApplication;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.control.AbstractControlActivity;
import cn.sdhuijin.healthexam.util.Util;

/**
 * FTP导出设置
 * 
 * 修改完成
 * 
 * @author sunping
 * 
 */
public class ExportSettingActivity extends AbstractControlActivity implements
		OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_export_setting);
		ExitApplication.getInstance().addActivity(this);

		addEditTextView(R.id.edt_ftp_address, "FTP地址");
		addEditTextView(R.id.edt_ftp_port, "端口号");
		addEditTextView(R.id.edt_ftp_path, "保存路径");
		addEditTextView(R.id.edt_ftp_user, "用户名");
		addEditTextView(R.id.edt_ftp_pass, "密码");

		setExportFileDefault();

		controlManager.setPropertiesPath(Environment
				.getExternalStorageDirectory() + "/health/ftp.txt");
		Log.i(this.getClass().getSimpleName(),
				"文件路径： " + Environment.getExternalStorageDirectory()
						+ "/health/ftp.txt");
		controlManager.toControl();

		this.setButtonListener(this, R.id.btn_back, R.id.btn_save);
	}

	/**
	 * 保存导出设置
	 */
	private void saveFtpSetting() {
		String ftpAddress = this.getControlValue(R.id.edt_ftp_address);
		String ftpPort = this.getControlValue(R.id.edt_ftp_port);
		String ftpPath = this.getControlValue(R.id.edt_ftp_path);
		String ftpUser = this.getControlValue(R.id.edt_ftp_user);
		String ftpPass = this.getControlValue(R.id.edt_ftp_pass);
		if (Util.allIsNotEmpty(ftpAddress, ftpPort, ftpPath, ftpUser, ftpPass)) {
			controlManager.toProperties();
			this.toastInformation(R.string.ftp_setting_success);
		} else {
			this.toastInformation(R.string.setting_not_null);
		}
	}

	/**
	 * 若导出参数所保存的文件不存在，则新建文件，写入默认参数
	 */
	private void setExportFileDefault() {
		Properties properties = null;
		File file = null;
		File dirFile = null;
		try {
			properties = new Properties();
			dirFile = new File(Environment.getExternalStorageDirectory()
					+ "/health/");
			if (!dirFile.exists()) {
				dirFile.mkdirs();
			}
			file = new File(Environment.getExternalStorageDirectory()
					+ "/health/ftp.txt");
			if (!file.exists()) {
				file.createNewFile();
				saveExportSetting(file, properties);
			} else {
				properties.load(new FileInputStream(file));
				String updateUrl = properties.getProperty("FTP地址");
				if (!Util.allIsNotEmpty(updateUrl)) {
					saveExportSetting(file, properties);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 保存默认设置
	 * 
	 * @param file
	 * @param properties
	 * @throws Exception
	 */
	private void saveExportSetting(File file, Properties properties)
			throws Exception {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		fileInputStream = new FileInputStream(file);
		properties.load(fileInputStream);
		// 检测网址
		properties.setProperty("FTP地址", "192.168.2.116");
		// 检测周期
		properties.setProperty("端口号", "21");
		properties.setProperty("保存路径", "/export.xls");
		properties.setProperty("用户名", "user1");
		properties.setProperty("密码", "123456");
		fileOutputStream = new FileOutputStream(
				Environment.getExternalStorageDirectory() + "/health/ftp.txt");
		// 保存
		properties.store(fileOutputStream, "导出设置参数信息");
	}

	@Override
	public void onClick(View v) {
		int clickId = (Integer) v.getId();
		switch (clickId) {
		case R.id.btn_save:
			saveFtpSetting();
			break;
		case R.id.btn_back:
			finish();
			break;
		}

	}
}
